/* Copyright (c) 2010-2020 Vadim Surov.
 */
package com.srv.client.widget;

import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.dnd.core.client.DND.Operation;
import com.sencha.gxt.dnd.core.client.DndDropEvent;
import com.sencha.gxt.dnd.core.client.DropTarget;
import com.sencha.gxt.widget.core.client.Dialog.PredefinedButton;
import com.sencha.gxt.widget.core.client.Portlet;
import com.sencha.gxt.widget.core.client.Resizable;
import com.sencha.gxt.widget.core.client.box.ConfirmMessageBox;
import com.sencha.gxt.widget.core.client.button.ToolButton;
import com.sencha.gxt.widget.core.client.container.PortalLayoutContainer;
import com.sencha.gxt.widget.core.client.event.HideEvent;
import com.sencha.gxt.widget.core.client.event.HideEvent.HideHandler;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
import com.sencha.gxt.widget.core.client.menu.CheckMenuItem;
import com.sencha.gxt.widget.core.client.menu.Item;
import com.sencha.gxt.widget.core.client.menu.Menu;
import com.srv.shared.Set;
import com.srv.shared.SetInfo;

public class SetDesignPortal extends PortalLayoutContainer {

	private SetView setView;

	public SetDesignPortal() {
		super(2);

		getElement().getStyle().setBackgroundColor("lightgrey");

		new DropTarget(this) {
			{
				setGroup("moveset");
				setOperation(Operation.COPY);
				setOverStyle("drag-ok");
			}
			@Override
			protected void onDragDrop(DndDropEvent event) {
				super.onDragDrop(event);
				Set set = (Set) event.getData();

				final Portlet portlet = new Portlet();
				portlet.setHeadingText("New Set");
				portlet.setWidth(500);
				portlet.setCollapsible(true);
				portlet.setAnimCollapse(false);
				final ToolButton toolButton = new ToolButton(ToolButton.GEAR, new SelectHandler() {

					private CheckMenuItem tabMenuItem;
					private CheckMenuItem listMenuItem;
					private CheckMenuItem sourceMenuItem;
					private Menu viewContextMenu = null;

					@Override
					public void onSelect(SelectEvent event) {

						if (viewContextMenu==null) {
							viewContextMenu = new Menu() {
								{
									add(tabMenuItem = menuItem(portlet, "Tabbed View"));
									add(listMenuItem = menuItem(portlet, "List View"));
									add(sourceMenuItem = menuItem(portlet, "Source View"));
									tabMenuItem.setChecked(true);
								}

								private CheckMenuItem menuItem(final Portlet portlet, final String item) {
									return new CheckMenuItem() {{
										setText(item);
										setGroup("view");
										addSelectionHandler(new SelectionHandler<Item>() {
											@Override
											public void onSelection(SelectionEvent<Item> event) {
												if (!setView.isSafeToClose()) {
													final ConfirmMessageBox confirmMessageBox = new ConfirmMessageBox (
															"Confirm message", 
															"Modified source code cannot be applyed. " +
															"Are you sure you want to leave the view anyway?");
													confirmMessageBox.setWidth(300);
													confirmMessageBox.addHideHandler(new HideHandler() {
														@Override
														public void onHide(HideEvent event) {
															if (confirmMessageBox.getHideButton() == 
																	confirmMessageBox.getButtonById(PredefinedButton.YES.name())) {
																ChangeView(portlet, item);
															}
														}
													});
													confirmMessageBox.show();	
												} else {
													ChangeView(portlet, item);
												}
											}

											void ChangeView(Portlet portlet, String item) {
												if (item.equals("Tabbed View")) {
													tabMenuItem.setChecked(true, true);
													setView = new SetTabPanel(new SetInfo(), setView.getCards());
												} else if (item.equals("List View")) {
													listMenuItem.setChecked(true, true);
													setView = new SetListPanel(new SetInfo(), setView.getCards());			
												} else if (item.equals("Source View")) {
													sourceMenuItem.setChecked(true, true);
													setView = new SetSourcePanel(new SetInfo(), setView.getCards());
												}
												portlet.setWidget((Widget)setView);
											}
										});
									}};
								}};
						}
						final ToolButton toolButton = (ToolButton)event.getSource();
						viewContextMenu.showAt(toolButton.getAbsoluteLeft(), 
								toolButton.getAbsoluteTop());
					}
				});

				portlet.getHeader().addTool(toolButton);
				portlet.getHeader().addTool(new ToolButton(ToolButton.CLOSE, new SelectHandler() {
					@Override
					public void onSelect(SelectEvent event) {
						portlet.removeFromParent();
					}
				}));

				setView = new SetTabPanel(new SetInfo(), set.getCards());
				portlet.add((Widget)setView);
				add(portlet, 0);	

				new Resizable(portlet, Resizable.Dir.SE);
			}
		};
	}
}
